package com.liyy.leetcode.editor.cn;

//题目编号:203
//题目标题:移除链表元素	
//题目标记:remove-linked-list-elements	

//题目描述:
//删除链表中等于给定值 val 的所有节点。 
//
// 示例: 
//
// 输入: 1->2->6->3->4->5->6, val = 6
//输出: 1->2->3->4->5
// 
// Related Topics 链表

import java.util.List;

public class RemoveLinkedListElements {
  
    public static void main(String[] args) {
       Solution solution = new RemoveLinkedListElements().new Solution();
    }
  
//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        //虚拟头结点，方便处理
        ListNode sentinel = new ListNode(0);
        sentinel.next = head;

        //迭代使用的指针
        ListNode prev = sentinel;

        while (prev != null && prev.next != null) {
            if (prev.next.val == val) {
                prev.next = prev.next.next;
            }else {
                // 不相等时的处理
                prev = prev.next;
            }
        }

        return sentinel.next;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}